﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;


public partial class Plublic_Cards : System.Web.UI.Page
{
    String StrConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    CardTypeHandler objCardTypeHandler = new CardTypeHandler();
    string cardTypeIUpdate;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {

            GetCardType();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        if (txtNameCard.Text != string.Empty)
        {
            CardType objCardType = new CardType();

            objCardType.Created = DateTime.Now.ToString();
            objCardType.Updated = DateTime.Now.ToString();
            objCardType.Isactive = "1";
            objCardType.Name = txtNameCard.Text.ToString();
            objCardType.Descriptions = txtDescription.Text.ToString();

            objCardTypeHandler.AddNewCardType(objCardType, StrConn);
            GetCardType();
            this.setText();
        }

    }

    protected void setText() 
    {
        this.txtNameCard.Text = "";
        this.txtDescription.Text = "";
    }

    protected void GetCardType() {

        List<CardType> objCardType = objCardTypeHandler.GetCardTypeList(StrConn);

        GRVCardType.DataSource = objCardType;
        GRVCardType.DataBind();
    
    }
    
    protected void GRVCardType_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName != String.Empty)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            cardTypeIUpdate =GRVCardType.DataKeys[index].Value.ToString();
            Session["cardTypeIUpdate"] = cardTypeIUpdate;
            
            switch (e.CommandName.ToString())
            {
                case "View":
                    BindTextBox(index);
                    
                    break;    

            }
        }

    }

    
    protected void BindTextBox(int key)
    {

      string cardTypeID = GRVCardType.DataKeys[key].Value.ToString();

      CardType objCardType = new CardType();
  
      objCardType =  objCardTypeHandler.GetCardTypeDetails(cardTypeID, StrConn);
      txtNameCard.Text = objCardType.Name;
      txtDescription.Text = objCardType.Descriptions;
      

    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {


        CardType objCardType = new CardType();

        string idUpdate = Session["cardTypeIUpdate"].ToString();
        objCardType = objCardTypeHandler.GetCardTypeDetails(idUpdate, StrConn);

        objCardType.Name = txtNameCard.Text.ToString();
        objCardType.Descriptions = txtDescription.Text.ToString();
        objCardType.Updated = DateTime.Now.ToString("MM/dd/yyyy");

        objCardTypeHandler.UpdateCardType(objCardType, StrConn);

        GetCardType();
        this.setText();
        
    }
}